package com.dkcrow.hadoop;

import com.dkcrow.hadoop.conf.MyConfiguration;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;

public class HdfsHandler {


    public static void main(String[] args) {
        // 파일 시스템 제어 객체 생성

        delete(MyConfiguration.getConf(), "/user/dkcrow/my_remote_msg");
    }

    public static void delete(Configuration conf, String pathString) {
        try {

            FileSystem hdfs = FileSystem.get(conf);

            // 경로 체크
            Path path = new Path(pathString);
            if (hdfs.exists(path)) {
                hdfs.delete(path, true);
            }


//            // 파일 저장
//            FSDataOutputStream outStream = hdfs.create(path);
//            outStream.writeUTF("안녕하세요 하둡씨.");
//            outStream.close();
//
//
//
//
//
//            System.out.println("## inputString:" + inputString);
            hdfs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void read(Configuration conf, String pathString) {
        FileSystem hdfs = null;
        try {
            System.out.println(pathString);
            hdfs = FileSystem.get(conf);
            Path path = new Path(pathString);

            FSDataInputStream inputStream = hdfs.open(path);
            String targetString = inputStream.readUTF();
            System.out.println(targetString);
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
}
